package com.louis.web.tag;

import org.springframework.util.StringUtils;

import com.louis.web.paging.Paging;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * @author Lijun jiang
 * @Date: May 5, 2011
 * @Time: 1:01:33 PM
 */

public class PagingTag extends TagSupport {
	private static final long	serialVersionUID	= 1L;
	private Paging<?>			paging;
	private String				url;
	private String				nextText			= "Next";
	private String				previousText		= "Previous";
	private int					showPage			= 5;
	private boolean				showFirstPage		= true;
	private boolean				showLastPage		= true;
	private boolean				showTotalPage		= true;
	private boolean				showNextPage		= true;
	private boolean				showPreviousPage	= true;
	private String				function;

	@Override
	public int doStartTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			if (this.paging != null) {
				out.write(createHtml());
			} else {
				out.write("pagingTool is null");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_BODY_INCLUDE;
	}

	private String createHtml() {
		analyzeUrl();
		int currentPage = this.paging.getCurrentPage();
		int totalPage = this.paging.getTotalPage();
		if (totalPage > 1) {
			String html = "<div class=\"pagingtool\">";
			if (this.showPreviousPage && this.paging.hasPreviousPage()) {
				html += "<a href=\"" + createUrl(this.paging.getPreviousPage()) + "\">" + this.previousText + "</a>";
			}
			if (this.showFirstPage) {
				if (currentPage != 1) {
					html += "<a href=\"" + createUrl(1) + "\">1</a>";
				} else {
					html += "<a href=\"javascript:void(0)\" class=\"currentPage\">1</a>";
				}
			}
			int startPage = currentPage - this.showPage;
			if (startPage <= 1) {
				startPage = 1;
				if (this.showFirstPage) {
					startPage = 2;
				}
			}
			int endPage = currentPage + this.showPage;
			if (endPage >= totalPage) {
				endPage = totalPage;
				if (this.showLastPage) {
					endPage = totalPage - 1;
				}
			}
			for (int i = startPage; i <= endPage; i++) {
				if (i == currentPage) {
					html += "<a href=\"javascript:void(0)\" class=\"currentPage\">" + i + "</a>";
				} else {
					html += "<a href=\"" + createUrl(i) + "\">" + i + "</a>";
				}
			}
			if (this.showLastPage) {
				if (currentPage != totalPage) {
					html += "<a href=\"" + createUrl(totalPage) + "\">" + totalPage + "</a>";
				} else {
					html += "<a href=\"javascript:void(0)\" class=\"currentPage\">" + totalPage + "</a>";
				}
			}
			if (this.showNextPage && this.paging.hasNextPage()) {
				html += "<a href=\"" + createUrl(this.paging.getNextPage()) + "\">" + this.nextText + "</a>";
			}
			html += "</div>";
			return html;
		}
		return "&nbsp;";
	}

	private String analyzeUrl() {
		int index = url.indexOf("?");
		if (index < 0) {
			url += "?currentPage=";
		} else {
			url += "&currentPage=";
		}
		return url;
	}

	private String createUrl(int pageIndex) {
		if (StringUtils.hasText(this.function)) {
			return "javascript:" + this.function + "('" + url + pageIndex + "')";
		}
		return url + pageIndex;
	}

	public Paging<?> getPage() {
		return paging;
	}

	public void setPagingTool(Paging<?> page) {
		this.paging = page;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getNextText() {
		return nextText;
	}

	public void setNextText(String nextText) {
		this.nextText = nextText;
	}

	public String getPreviousText() {
		return previousText;
	}

	public void setPreviousText(String previousText) {
		this.previousText = previousText;
	}

	public int getShowPage() {
		return showPage;
	}

	public void setShowPage(int showPage) {
		this.showPage = showPage;
	}

	public boolean isShowFirstPage() {
		return showFirstPage;
	}

	public void setShowFirstPage(boolean showFirstPage) {
		this.showFirstPage = showFirstPage;
	}

	public boolean isShowLastPage() {
		return showLastPage;
	}

	public void setShowLastPage(boolean showLastPage) {
		this.showLastPage = showLastPage;
	}

	public boolean isShowTotalPage() {
		return showTotalPage;
	}

	public void setShowTotalPage(boolean showTotalPage) {
		this.showTotalPage = showTotalPage;
	}

	public boolean isShowNextPage() {
		return showNextPage;
	}

	public void setShowNextPage(boolean showNextPage) {
		this.showNextPage = showNextPage;
	}

	public boolean isShowPreviousPage() {
		return showPreviousPage;
	}

	public void setShowPreviousPage(boolean showPreviousPage) {
		this.showPreviousPage = showPreviousPage;
	}

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}
}